package cn.youqu.happy.module.youqu.controller.app.userAssess;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userAssess.vo.UserAssessPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userAssess.vo.AppUserAssessPageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userAssess.vo.AppUserAssessPublishReqVO;
import cn.youqu.happy.module.youqu.controller.app.userAssess.vo.AppUserAssessRespVO;
import cn.youqu.happy.module.youqu.convert.userAssess.UserAssessConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userAssess.UserAssessDO;
import cn.youqu.happy.module.youqu.service.userAssess.UserAssessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户评价")
@RestController
@RequestMapping("/youqu/user-assess")
@Validated
public class AppUserAssessController {

    @Resource
    private UserAssessService userAssessService;

    @PostMapping("/publish")
    @ApiOperation("发表用户评价")
    @PreAuthenticated
    public CommonResult<Integer> publishUserAssess(@Valid @RequestBody AppUserAssessPublishReqVO publishReqVO) {
        Long loginUserId = getLoginUserId();
        return success(userAssessService.publishUserAssess(publishReqVO, loginUserId));
    }


    @DeleteMapping("/delete")
    @ApiOperation("删除用户评价")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<Boolean> deleteUserAssess(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        userAssessService.deleteUserAssess(id, loginUserId);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户评价")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated
    public CommonResult<AppUserAssessRespVO> getUserAssess(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        UserAssessDO userAssess = userAssessService.getUserAssess(id, loginUserId);
        return success(UserAssessConvert.INSTANCE.convert0(userAssess));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户评价列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated
    public CommonResult<List<AppUserAssessRespVO>> getUserAssessList(@RequestParam("ids") Collection<Integer> ids) {
        Long loginUserId = getLoginUserId();
        List<UserAssessDO> list = userAssessService.getUserAssessList(ids, loginUserId);
        return success(UserAssessConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户评价分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppUserAssessRespVO>> getUserAssessPage(@Valid AppUserAssessPageReqVO pageVO) {
        Long loginUserId = getLoginUserId();
        UserAssessPageReqVO convert = UserAssessConvert.INSTANCE.convert(pageVO);
        PageResult<UserAssessDO> pageResult = userAssessService.getUserAssessPage(convert, loginUserId);
        return success(UserAssessConvert.INSTANCE.convertPage0(pageResult));
    }


}
